<template>
  <custom-dialog
    ref="dialog" :title="modalTitle"
    :before-confirm="beforeConfirm" :on-confirm="onConfirm"
  >
    <el-form ref="form" :model="info" :rules="rules" label-width="120px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="info.projectName"></el-input>
      </el-form-item>
      <el-form-item label="建设单位" prop="constructionUnit">
        <el-select v-model="info.constructionUnit" class="width-100">
          <el-option v-for="option in enterpriseOptions" :key="option.id" :label="option.enterpriseName" :value="option.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目类别">
        <dictionary-picker dic-code="XIANG_MU_LEI_XING" v-model="info.itemCategory"></dictionary-picker>
      </el-form-item>
      <el-form-item label="项目备案时间">
        <el-date-picker class="width-100" v-model="info.filingTime" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
      <el-form-item label="政府补助金额">
        <el-input v-model="info.subsidyAmount" type="number">
          <span slot="append">万元</span>
        </el-input>
      </el-form-item>
      <el-form-item label="总投资">
        <el-input v-model="info.totalInvestment" type="number">
          <span slot="append">万元</span>
        </el-input>
      </el-form-item>
    </el-form>
  </custom-dialog>
</template>

<script>
import CustomDialog from '@/components/custom-dialog'
import { saveOrUpdate } from '@/api/energySavingProject'
import DictionaryPicker from '@/components/dictionary-picker'
import { getListEnterprise } from '@/api/enterprose'

const api = {
  saveOrUpdate
}
export default {
  name: 'operate-modal',
  components: { DictionaryPicker, CustomDialog },
  props: {
    modalTitle: String,
    modalData: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      enterpriseOptions: [],
      info: {
        // 主键
        id: '',
        // 项目类别
        itemCategory: '',
        // 建设单位
        constructionUnit: '',
        // 项目名称
        projectName: '',
        // 项目备案时间
        filingTime: '',
        // 政府补助金额
        subsidyAmount: '',
        // 总投资
        totalInvestment: '',
      },
      rules: {
        projectName: [{ required: true, message: '必填项' }],
        constructionUnit: [{ required: true, message: '必填项' }],
      },
    }
  },
  methods: {
    open () {
      this.info = {
        ...this.$options.data().info,
        ...this.modalData
      }
      if (this.info.constructionUnit) {
        this.info.constructionUnit = Number(this.info.constructionUnit)
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
      this.getListEnterprise()
      this.$nextTick(() => {
        this.$refs.dialog.open()
      })
    },
    beforeConfirm () {
      return this.$refs.form.validate()
    },
    getListEnterprise () {
      getListEnterprise().then(res => {
        if (res.code === 200) {
          this.enterpriseOptions = res.data
        }
      })
    },
    onConfirm (close) {
      const params = { ...this.info }
      if (!params.id) {
        delete params.id
      }
      return api.saveOrUpdate(params).then(res => {
        if (res.code === 200) {
          this.$emit('save-success')
          this.$message.success('保存成功')
          close()
        }
      })
    },
  }
}
</script>

<style scoped>

</style>
